package tests;

import common.WebAutoTest.TestUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
 * @Author: wenjingyuan
 * @Date: 2023/09/18/10:58
 * @Description:
 */
public class BlogAdd extends TestUtils {
    public static ChromeDriver driver=new ChromeDriver();
    @Test
    @BeforeAll
    static void init() {
        driver.get("http://121.40.90.244:8080/blog_add.html");
    }
    /**
     * 输入标题和正文，点击“发布文章”
     * 是否跳转到个人博客列表
     */

    //登录状态下，添加成功
    @ParameterizedTest
    @CsvSource({"add1,http://121.40.90.244:8080/myblog_list.html"})
    void blog_add_success(String title,String myblog_list_url) throws InterruptedException {
        //先登录
        //login.login("admin","12345678","http://121.40.90.244:8080/myblog_list.html");
        //到达博客添加页
        driver.get("http://121.40.90.244:8080/blog_add.html");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入标题和正文
        driver.findElement(By.cssSelector("#title")).sendKeys(title);
        driver.findElement(By.cssSelector("#editorDiv > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i")).click();
        //点击按钮提交
        driver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        Thread.sleep(100);//等待弹窗
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        String expect_alert = "添加成功，是否继续添加文章？";
        String actual_alert = alert.getText();
        Assertions.assertEquals(actual_alert,expect_alert);
        //选择取消不再添加
        alert.dismiss();
        //跳转到个人博客列表页
        //获取到当前的url并验证
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals(myblog_list_url ,cur_url);
    }


    //登录状态，未输入标题或正文，弹出提示
    @ParameterizedTest
    @CsvSource({"'',http://121.40.90.244:8080/myblog_list.html"})
    void blog_add_failed(String title,String myblog_list_url) throws InterruptedException {
        //先登录
        //login.login("admin","12345678","http://121.40.90.244:8080/myblog_list.html");
        //到达博客修改页（修改id为8的文章）
        driver.get("http://121.40.90.244:8080/blog_add.html");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入标题和正文
        driver.findElement(By.cssSelector("#title")).sendKeys(title);
        //通过JS将标题输入
        //((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        driver.findElement(By.cssSelector("#editorDiv > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i")).click();
        //点击按钮提交
        driver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        Thread.sleep(100);//等待弹窗
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        if(title==""){
            String expect_alert = "请先输入标题！";
            String actual_alert = alert.getText();
            Assertions.assertEquals(actual_alert,expect_alert);
            alert.accept();
        }

    }
    @AfterAll
    @Test
    static void quit() {
        driver.quit();
    }

}
